using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;
using Terminal.Advanced_AEUC_settings_controls.Settings_Interfaces;
using Terminal.Advanced_AEUC_settings_controls.Setting_Advanced_Control;
using System.IO;

namespace Terminal.Advanced_AEUC_settings_controls.Settings_Editing_Forms
{
    /// <summary>
    /// Form managing control reordering on a selected tab page
    /// </summary>
    public partial class CtrlReOrder : Form
    {
        AEUCTabbedParentControl prntAssociate;
        /// <summary>
        /// Public contructor
        /// </summary>
        /// <param name="iprntAssociate">AEUCTabbedParentControl reference</param>
        public CtrlReOrder(AEUCTabbedParentControl iprntAssociate)
        {
            prntAssociate = iprntAssociate;
            
            InitializeComponent();

            lbControlList.DisplayMember = "AEUC_DisplayName";
            
            for (int i = 0; i < prntAssociate.SelectedTab.Controls.Count; i++)
            {
                lbControlList.Items.Add(prntAssociate.SelectedTab.Controls[i]);
            }
        }

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            if (lbControlList.SelectedIndices.Count > 0)
            {
                
                int[] indxs_sel = new int[lbControlList.SelectedIndices.Count];
                lbControlList.SelectedIndices.CopyTo(indxs_sel,0);
                Object[] objs_sel = new Object[lbControlList.SelectedItems.Count];
                lbControlList.SelectedItems.CopyTo(objs_sel,0);

                for (int i = 0; i < indxs_sel.Length; i++)
                {
                    if (indxs_sel[0] > 0)
                    {
                        Object objTmp = objs_sel[i];
                        int crn_idx = indxs_sel[i] - 1;
                        lbControlList.Items.Remove(objTmp);
                        lbControlList.Items.Insert(crn_idx, objTmp);

                        lbControlList.SelectedIndices.Add(crn_idx);
                    }
                }
            }
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            if (lbControlList.SelectedIndices.Count > 0)
            {

                int[] indxs_sel = new int[lbControlList.SelectedIndices.Count];
                lbControlList.SelectedIndices.CopyTo(indxs_sel, 0);
                Object[] objs_sel = new Object[lbControlList.SelectedItems.Count];
                lbControlList.SelectedItems.CopyTo(objs_sel, 0);

                for (int i = indxs_sel.Length - 1; i >= 0; i--)
                {
                    if (indxs_sel[indxs_sel.Length - 1] < lbControlList.Items.Count - 1)
                    {
                        Object objTmp = objs_sel[i];
                        int crn_idx = indxs_sel[i] + 1;
                        lbControlList.Items.Remove(objTmp);
                        lbControlList.Items.Insert(crn_idx, objTmp);

                        lbControlList.SelectedIndices.Add(crn_idx);
                    }
                }
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            prntAssociate.SelectedTab.Controls.Clear();
            for (int i = 0; i < lbControlList.Items.Count; i++)
            {
                prntAssociate.SelectedTab.Controls.Add((Control)lbControlList.Items[i]);
            }

            DialogResult = DialogResult.OK;
        }
    }
}